
package com.hxkj.controller;

import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.FunctionMenu;
import com.hxkj.domain.po.FunctionSite;
import com.hxkj.service.UserService;
import com.hxkj.token.po.LoginToken;
import com.platform.common.domain.result.ResultDataStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;


/**
 * 用户控制器
 * <p>
 * Created by chenwei on 2018/5/15.
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Autowired
    private UserService userService;


    /**
     * 登录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<LoginToken>> login(@RequestBody AuthUser authUser) {
        return () -> userService.login(authUser);
    }


    /**
     * 获取单个用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/account/get", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AuthUser>> getUser(@RequestBody String account) {
        return () -> userService.getUser(account);
    }


    /**
     * 根据账户名查找角色
     *
     * @param account
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/role/get", consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Set<String>>> findRolesByAccount(@RequestBody String account) {
        return () -> userService.findRolesByAccount(account);
    }

    /**
     * 根据账户名查询权限
     *
     * @param account
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/permissions/get", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultDataStruct<Set<String>> findPermissionsByAccount(@RequestBody String account) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        try {
            Set<String> permissions = userService.findPermissionsByAccount(account);
            resultDataStruct.setMessage("成功");
            resultDataStruct.setCode(200);
            resultDataStruct.setData(permissions);
        } catch (Exception e) {
            resultDataStruct.setCode(400);
            resultDataStruct.setMessage(e.getMessage());
        }
        return resultDataStruct;
    }


    /**
     * 通过账户查询菜单
     *
     * @param functionSite
     * @return
     */
    @RequestMapping(value = "/menu/get", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultDataStruct<List<FunctionMenu>> findFunctionMenuByAccount(@RequestBody FunctionSite functionSite) {
        ResultDataStruct<List<FunctionMenu>> dataStruct = new ResultDataStruct<>();
        try {
            List<FunctionMenu> FunctionMenu = userService.findFunctionMenuByAccount(functionSite);
            dataStruct.setCode(200);
            dataStruct.setData(FunctionMenu);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取功能失败:{}", e.getMessage());
            dataStruct.setCode(400);
            dataStruct.setMessage(e.getMessage());
        }
        return dataStruct;
    }


    /**
     * 通过token获取用户信息成功
     *
     * @param accessToken
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/token/id", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AuthUser>> getUserInfoByToken(@RequestBody String accessToken) {
        return () -> userService.getUserInfoByTokenRemote(accessToken);
    }


    /**
     * 通过id获取用户信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/id/get", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AuthUser>> getUserInfoById(@RequestBody String userId) {
        return () -> userService.getUserInfoByIdRemote(userId);
    }

}

